#!/user/bin/python
# -*- coding: UTF-8 -*
from http.server import BaseHTTPRequestHandler, HTTPServer
import json
import hashlib
import threading

class MyRequestHandler1(BaseHTTPRequestHandler):
    def _send_response(self, response_code, content_type, response_content):
        self.send_response(response_code)
        self.send_header('Content-type', content_type)
        self.end_headers()
        self.wfile.write(response_content)

    def do_POST(self):
        content_length = int(self.headers['Content-Length'])
        post_data = self.rfile.read(content_length).decode('utf-8')

        try:
            # 在这里可以对接收到的 JSON 数据进行处理
            json_data = json.loads(post_data)
            json_string = json.dumps(json_data, indent=2)
            #打印接收到的注册数据
            print('收到终端注册包数据:')
            print(json_string)
            # 处理逻辑...

            # 创建 MD5 加密对象
            md5_hash = hashlib.md5()

            # 更新对象以包含要加密的数据
            md5_hash.update(json_string.encode('utf-8'))

            # 获取 MD5 加密结果
            hashed_result = md5_hash.hexdigest()

            # 打印结果
            print(f"MD5 加密结果: {hashed_result}")

            # 构造要返回的 JSON 数据
            response_data = '{"cid":"%s","err_no":"0"}' % hashed_result
            response_content = response_data.encode('utf-8')

            # 发送 HTTP 响应
            self._send_response(200, 'application/json', response_content)
        except json.JSONDecodeError:
            # JSON 解析错误
            error_response = '{"err_no":"1"}'
            error_content = error_response.encode('utf-8')
            self._send_response(400, 'application/json', error_content)

class MyRequestHandler2(BaseHTTPRequestHandler):
    # 类似地定义第二个处理器类...

    def _send_response(self, response_code, content_type, response_content):
        self.send_response(response_code)
        self.send_header('Content-type', content_type)
        self.end_headers()
        self.wfile.write(response_content)

    def do_POST(self):
        content_length = int(self.headers['Content-Length'])
        post_data = self.rfile.read(content_length).decode('utf-8')

        try:
            # 在这里可以对接收到的 JSON 数据进行处理
            json_data = json.loads(post_data)
            json_string = json.dumps(json_data, indent=2)
            #打印接收到的注册数据
            print('收到终端预下发请求:')
            print(json_string)
            # 处理逻辑...

            scid = json_data["scid"]
            dcid = json_data["dcid"]
            # 打印结果
            print(f"scid: {scid}")
            print(f"dcid: {dcid}")

            # 构造要返回的 JSON 数据
            response_data = '{"err_no":"0"}'
            response_content = response_data.encode('utf-8')

            # 发送 HTTP 响应
            self._send_response(200, 'application/json', response_content)
        except json.JSONDecodeError:
            # JSON 解析错误
            error_response = '{"err_no":"1"}'
            error_content = error_response.encode('utf-8')
            self._send_response(400, 'application/json', error_content)
            
            

            
def run_server(port, handler_class):
    server_address = ('', port)
    httpd = HTTPServer(server_address, handler_class)
    print(f'正在启动端口为{port}的服务器...')
    httpd.serve_forever()

def run_servers():
    # 为每个服务器创建线程
    threads = []
    ports_and_handlers = [(19999, MyRequestHandler1), (12340, MyRequestHandler2)]

    for port, handler_class in ports_and_handlers:
        thread = threading.Thread(target=run_server, args=(port, handler_class))
        threads.append(thread)

    # 启动所有线程
    for thread in threads:
        thread.start()

    # 等待所有线程完成
    for thread in threads:
        thread.join()

if __name__ == '__main__':
    run_servers()


